package com.xhc.business.controller.admin;

import com.xhc.server.dto.*;
import com.xhc.server.service.CourseCategoryService;
import com.xhc.server.service.CourseService;
import com.xhc.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author: XHC
 * @Date: 2023-12-19-17:58
 * @Description: 吃番茄不吃番茄酱
 * @注释:
 */
@RestController
@RequestMapping("/admin/course")
public class CourseController {

    private static final Logger LOG = LoggerFactory.getLogger(CourseController.class);
    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;

    @Resource
    private CourseCategoryService courseCategoryService;

    /**
     * 列表【查询】
     * @param pageDto
     * @return
     */
    @RequestMapping("/list")
    public ResponseDto list(@RequestBody CoursePageDto pageDto){
        ResponseDto responseDto = new ResponseDto();
        courseService.list(pageDto);
        responseDto.setContent(pageDto);
         return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     * @param courseDto
     * @return
     */
    @RequestMapping("/save")
    public ResponseDto save(@RequestBody CourseDto courseDto){
        // 保存校验
        ValidatorUtil.require(courseDto.getName(), "名称");
        ValidatorUtil.length(courseDto.getName(), "名称", 1, 50);
        ValidatorUtil.length(courseDto.getSummary(), "概述", 1, 2000);
        ValidatorUtil.length(courseDto.getImage(), "封面", 1, 100);

        ResponseDto responseDto = new ResponseDto();
        courseService.save(courseDto);
        responseDto.setContent(courseDto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        courseService.delete(id);
        return responseDto;
    }

    /**
     * 查找课程下所有分类
     * @param courseId
     * @return
     */
    @PostMapping("/list-category/{courseId}")
    public ResponseDto listCategory(@PathVariable(value = "courseId") String courseId){
        ResponseDto responseDto = new ResponseDto();
        List<CourseCategoryDto> dtoList = courseCategoryService.listByCourse(courseId);
        responseDto.setContent(dtoList);
        return responseDto;
    }


  @GetMapping("/find-content/{id}")
    public ResponseDto findContent(@PathVariable String id){
      ResponseDto responseDto = new ResponseDto();
      CourseContentDto contentDto = courseService.findContent(id);
      responseDto.setContent(contentDto);
      return responseDto;
  }


   @PostMapping("/save-content")
    public ResponseDto saveContent(@RequestBody CourseContentDto contentDto){
       ResponseDto responseDto = new ResponseDto();
       courseService.saveContent(contentDto);
       return  responseDto;
   }

   @RequestMapping(value = "/sort")
    public ResponseDto sort(@RequestBody SortDto sortDto){
        LOG.info("更新排序");
       ResponseDto responseDto = new ResponseDto();
       courseService.sort(sortDto);
       return responseDto;
   }

}
